<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>企业员工管理-编辑部门</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="static/css/font.css">
    <link rel="stylesheet" href="static/css/xadmin.css">
    <script type="text/javascript" src="static/js/jquery-3.3.1.js"></script>
    <script type="text/javascript" src="static/lib/layui/layui.js"></script>
    <script type="text/javascript" src="static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>部门管理</a>
        <a>
          <cite>部门编辑</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>

<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form" action="">
        <!--id的隐藏域-->
        <input type="hidden" value="" id="dIdInput">
        <div class="layui-form-item">
            <label class="layui-form-label">科室名称</label>
            <div class="layui-input-block">
                <input name="name" id="dNameInput" lay-verify="dNameVerify" autocomplete="off" class="layui-input"
                       type="text" value="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">科室级别</label>
            <div class="layui-input-block">
                <input name="level" id="dLevelInput" lay-verify="dLevelVerify" autocomplete="off" class="layui-input"
                       type="text" value="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上级科室</label>
            <div class="layui-input-block">
                <input name="gsid" id="dGsidInput" lay-verify="dGsidVerify" autocomplete="off" class="layui-input"
                       type="text" value="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门备注</label>
            <div class="layui-input-block">
                <textarea name="remark" id="dRemarkInput" lay-verify="dRemarkVerify" autocomplete="off"
                          placeholder="请输入部门备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门职责</label>
            <div class="layui-input-block">
                <textarea name="respContent" id="respContent" lay-verify="respContentVerify" autocomplete="off"
                          placeholder="请输入部门职责" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="department-edit-btn">确定</button>
            </div>
        </div>
    </form>
</div>

<script>

    //加载参数
    window.onload = function () {
        //加载部门id
        $("#dIdInput").val(decodeURI(getQueryVariable("dId")));
        //加载部门名称
        $("#dNameInput").val(decodeURI(getQueryVariable("dName")));
        //加载部门级别
        $("#dLevelInput").val(decodeURI(getQueryVariable("dLevel")));
        //加载上级部门级别
        $("#dGsidInput").val(decodeURI(getQueryVariable("dGsid")));
        //加载部门备注
        $("#dRemarkInput").val(decodeURI(getQueryVariable("dRemark")));
        // 加载部门职责
        $("#respContent").val(decodeURI(getQueryVariable("respContent")));
        // 加载职责 id
        const respId = decodeURI(getQueryVariable("respId"));
    }

    //获取url参数
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var index = query.indexOf("?");
        query = query.substr(index + 1, query.length);
        // console.log(query);
        var vars = query.split("&");
        // console.log(vars);
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            // console.log(pair[0]);
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return false;
    }

    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;
        //自定义验证规则
        form.verify({
            dNameVerify: function (value) {
                // 验证特殊字符
                var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");

                value = value.replace(" ", "");
                if (value.length == 0) {
                    return '部门名称不能为空';
                }
                if (value.length > 200) {
                    return '部门名称过长';
                }
                if (pattern.test(value)) {
                    return '部门名称不能包含特殊字符';
                }
            },
            dRemarkVerify: function (value) {
                // 验证特殊字符
                var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
                value = value.replace(" ", "");
                if (value.length == 0) {
                    return '部门备注不能为空';
                }
                if (value.length > 200) {
                    return '部门备注过长';
                }
                // if(pattern.test(value)){
                //     return '部门备注不能包含特殊字符';
                // }
            },
            respContentVerify: function (value) {
                // 验证特殊字符
                var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
                value = value.replace(" ", "");
                if (value.length == 0) {
                    return '部门职责不能为空';
                }
                if (value.length > 200) {
                    return '部门职责过长';
                }
            }
        });


        // 监听提交
        form.on('submit(department-edit-btn)', function (data) {
            const respId = decodeURI(getQueryVariable("respId"));
            $.ajax({
                type: 'post',
                url: 'department/updateByPrimaryKey.do',
                data: {
                    "id": parseInt($("#dIdInput").val()), "name": data.field.name.replace(" ", "")
                    , "level": data.field.level.replace(" ", ""), "gsid": data.field.gsid.replace(" ", "")
                    , "remark": data.field.remark, "respContent": data.field.respContent, "respId": respId
                },
                dataType: 'json',
                success: function (result) {
                    if (result.code == 100) {
                        layer.alert("修改成功", {
                                title: '修改信息'
                            },
                            function () {
                                window.parent.location.reload();
                            });
                    } else {
                        layer.msg(result.message, {icon: 5, anim: 6});
                    }
                }
            });
            return false;
        });
    });
</script>

</body>

</html>